package com.rq.base.util;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.rq.base.widget.pickview.configure.PickerOptions;
import com.rq.base.widget.pickview.listener.OnOptionsSelectListener;
import com.rq.base.widget.pickview.listener.OnTimeSelectListener;
import com.rq.base.widget.pickview.view.OptionsPickerView;
import com.rq.base.widget.pickview.view.TimePickerView;
import com.rq.base.R;
import com.rq.base.ui.BaseController;
import com.rq.base.widget.pop.CustomPopWindow;

import java.util.Date;
import java.util.List;

import androidx.annotation.NonNull;

import static com.rq.base.widget.pickview.configure.PickerOptions.TYPE_PICKER_OPTIONS;


public class PopUtil {


    public static void showTime(Context context, final View clickFrom, final OnTimeListener listener, String... tag) {
        PickerOptions pickerOptions = new PickerOptions(TYPE_PICKER_OPTIONS);
        pickerOptions.context = context;
        pickerOptions.textContentConfirm = "确认";
        pickerOptions.textContentCancel = "取消";
        if (tag != null && tag.length > 0) {
            pickerOptions.textContentTitle = tag[0];
        }
        pickerOptions.cancelable = true;
        pickerOptions.isDialog = true;
        pickerOptions.type = new boolean[]{false, false, false, true, true, false};
        pickerOptions.timeSelectListener = new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (listener != null) listener.onChoice(clickFrom, date);
            }
        };
        TimePickerView optionView = new TimePickerView(pickerOptions);
        optionView.getDialog().getWindow().setGravity(Gravity.BOTTOM);
        optionView.getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        optionView.show();
    }

    public static void showTimeAll(Context context, final View clickFrom, final OnTimeListener listener) {
        PickerOptions pickerOptions = new PickerOptions(TYPE_PICKER_OPTIONS);
        pickerOptions.context = context;
        pickerOptions.textContentConfirm = "确认";
        pickerOptions.textContentCancel = "取消";
        pickerOptions.type = new boolean[]{true, true, true, true, true, true};
        pickerOptions.cancelable = true;
        pickerOptions.isDialog = true;
        pickerOptions.timeSelectListener = new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                if (listener != null) listener.onChoice(clickFrom, date);
            }

        };
        TimePickerView optionView = new TimePickerView(pickerOptions);
        optionView.getDialog().getWindow().setGravity(Gravity.BOTTOM);
        optionView.getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        optionView.show(clickFrom, true);
    }

    //    public static void showSearch(final Activity context, int i) {
//        final View contentView = LayoutInflater.from(context).inflate(R.layout.pop_search, null);
//        contentView.setBackground(null);
//        final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(context)
//                .setView(contentView).enableBackgroundDark(true).setOutsideTouchable(false).enableOutsideTouchableDissmiss(false)
//                .setOnDissmissListener(new PopupWindow.OnDismissListener() {
//                    @Override
//                    public void onDismiss() {
//                    }
//                })
//                .sizeAs(null).create();
//        final OnTimeListener listener = new OnTimeListener() {
//            @Override
//            public void onChoice(View clickFrom, Date date) {
//                ((TextView) clickFrom).setText(StringUtil.getTimeFormat(date.getTime(), "yyyy-MM-dd"));
//            }
//        };
//        View.OnClickListener clickListener = new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                showTime(context, v, listener);
//            }
//        };
//        contentView.findViewById(R.id.btn_time_start).setOnClickListener(clickListener);
//        contentView.findViewById(R.id.btn_time_end).setOnClickListener(clickListener);
//        popWindow.showAtLocation(context.getWindow().getDecorView(), Gravity.CENTER);
//    }
//
//    public static CustomPopWindow showPop(final BaseController from, Class<? extends BaseController> ctl) {
//        BaseController controller = null;
//        try {
//            controller = ctl.newInstance();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (InstantiationException e) {
//            e.printStackTrace();
//        }
//        if (controller == null) {
//            LOG.e("PopUtil", "showPop.32:");
//            return null;
//        }
//        View contentView = controller.inflateRootView(LayoutInflater.from(from.getContext()));
//        contentView.setBackground(null);
//        controller.setContext((BaseActivity) from.getContext());
//        controller.setViewRoot(contentView);
//        controller.onViewCreated(null);
//        controller.setPopWindowParent(from);
//        final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(from.getContext())
//                .setView(contentView)
//                .sizeAs(null).create();
//        from.setPopWindowControler(controller);
//        controller.setPopWindow(popWindow.getPopupWindow());
//        popWindow.getPopupWindow().setClippingEnabled(false);
//        popWindow.showAtLocation(from.getContext().getWindow().getDecorView(), Gravity.CENTER);
//        return popWindow;
//    }
//
    public interface OnChoiceListener<T> {
        void onChoice(View viewClick, T select, int position);
    }

    /**
     * @param <T> IPickerViewData
     */
    public static <T> void showPopChoice(final Context context, final List<T> data, final View clickFrom, final OnChoiceListener<T> listener) {
        AppUtils.hideInput(context);
        PickerOptions pickerOptions = new PickerOptions(TYPE_PICKER_OPTIONS);
        pickerOptions.context = context;
        pickerOptions.textContentConfirm = "确认";
        pickerOptions.textContentCancel = "取消";
        pickerOptions.cancelable = true;
        pickerOptions.isDialog = true;
        pickerOptions.optionsSelectListener = new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                if (listener != null) listener.onChoice(clickFrom, data.get(options1), options1);
            }
        };
        OptionsPickerView optionView = new OptionsPickerView(pickerOptions);
        optionView.setPicker(data);
        optionView.getDialog().getWindow().setGravity(Gravity.BOTTOM);
        optionView.getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        optionView.show();
    }

    /**
     * @param <T> IPickerViewData
     */
    public static <T> void showPopChoice(final BaseController controller, final List<T> data, final View clickFrom, final OnChoiceListener<T> listener) {
        showPopChoice(controller.getContext(), data, clickFrom, listener);
    }
//
//    public static void showTime(Context context, final View clickFrom, final OnTimeListener listener) {
//        PickerOptions pickerOptions = new PickerOptions(TYPE_PICKER_OPTIONS);
//        pickerOptions.context = context;
//        pickerOptions.textContentConfirm = "确认";
//        pickerOptions.textContentCancel = "取消";
//        pickerOptions.cancelable = true;
//        pickerOptions.timeSelectListener = new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//                if (listener != null) listener.onChoice(clickFrom, date);
//            }
//
//        };
//        TimePickerView optionView = new TimePickerView(pickerOptions);
//        optionView.show(clickFrom, true);
//    }
//
//    public static void showTimeAll(Context context, final View clickFrom, final OnTimeListener listener) {
//        PickerOptions pickerOptions = new PickerOptions(TYPE_PICKER_OPTIONS);
//        pickerOptions.context = context;
//        pickerOptions.textContentConfirm = "确认";
//        pickerOptions.textContentCancel = "取消";
//        pickerOptions.type = new boolean[]{true, true, true, true, true, true};
//        pickerOptions.cancelable = true;
//        pickerOptions.timeSelectListener = new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//                if (listener != null) listener.onChoice(clickFrom, date);
//            }
//
//        };
//        TimePickerView optionView = new TimePickerView(pickerOptions);
//        optionView.show(clickFrom, true);
//    }
//
//    public static void showTimeYear(Context context, final View clickFrom, final OnTimeListener listener) {
//        PickerOptions pickerOptions = new PickerOptions(TYPE_PICKER_OPTIONS);
//        pickerOptions.context = context;
//        pickerOptions.textContentConfirm = "确认";
//        pickerOptions.textContentCancel = "取消";
//        pickerOptions.type = new boolean[]{true, true, false, false, false, false};
//        pickerOptions.cancelable = true;
//        pickerOptions.timeSelectListener = new OnTimeSelectListener() {
//            @Override
//            public void onTimeSelect(Date date, View v) {
//                if (listener != null) listener.onChoice(clickFrom, date);
//            }
//
//        };
//        TimePickerView optionView = new TimePickerView(pickerOptions);
//        optionView.show(clickFrom, true);
//    }

    public interface OnTimeListener {

        void onChoice(View clickFrom, Date date);
    }

    public interface OnPopClickListener<T> {
        void onPopClick(int position, T data, View clickFrom);
    }

    public interface OnPhotoFromSelectListener {
        int SELECT_PHOTO = 1;
        int SELECT_TAKE = 2;
        int SELECT_VIDEO = 3;
        int CANCEL = 0;

        boolean onSelect(int select);
    }

    public static CustomPopWindow getPhotoWindow(Context context, @NonNull final OnPhotoFromSelectListener listener, boolean showVideoOpt) {
        AppUtils.hideInput(context);
        View contentView = LayoutInflater.from(context).inflate(R.layout.pop_photo, null);

        final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(contentView)
                .size(AppUtils.getScreenWidth(), AppUtils.getScreenHeight() + AppUtils.getStatusBarHeight()).create();
        if (showVideoOpt) {
            contentView.findViewById(R.id.tv_take_video).setVisibility(View.VISIBLE);
            contentView.findViewById(R.id.tv_take_video).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener.onSelect(OnPhotoFromSelectListener.SELECT_VIDEO)) {
                        popWindow.dissmiss();
                    }
                }
            });
        }
        contentView.findViewById(R.id.tv_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener.onSelect(OnPhotoFromSelectListener.CANCEL)) {
                    popWindow.dissmiss();
                }
            }
        });
        contentView.findViewById(R.id.tv_take).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener.onSelect(OnPhotoFromSelectListener.SELECT_TAKE)) {
                    popWindow.dissmiss();
                }
            }
        });
        contentView.findViewById(R.id.tv_photo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener.onSelect(OnPhotoFromSelectListener.SELECT_PHOTO)) {
                    popWindow.dissmiss();
                }
            }
        });
        contentView.findViewById(R.id.v_empty).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popWindow.dissmiss();
            }
        });
        LOG.e("PopUtil", "getPhotoWindow.165:" + AppUtils.getScreenHeight());
        contentView.measure(0, 0);
        LOG.e("PopUtil", "getPhotoWindow.166:" + contentView.getMeasuredHeight());


        popWindow.showAtLocation(((Activity) context).getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);//AppUtils.getStatusBarHeight()
        return popWindow;
    }

    public interface BelowViewHelper {

        int getLayoutId();

        void setClick(View contentView, CustomPopWindow popWindow);
    }

    public static CustomPopWindow showBelow(Context context, View topView, BelowViewHelper belowViewHelper) {
        View contentView = LayoutInflater.from(context).inflate(belowViewHelper.getLayoutId(), null);

        final CustomPopWindow popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(contentView)
                .size(AppUtils.getScreenWidth(), AppUtils.getScreenHeight() + AppUtils.getStatusBarHeight()).create();
        if (contentView != null) {
            belowViewHelper.setClick(contentView, popWindow);
        }
        contentView.measure(0, 0);
        popWindow.showAsDropDown(topView);//AppUtils.getStatusBarHeight()
        return popWindow;
    }

}
